﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.IO;
using WCF;

namespace geekmeet
{
    /// <summary>
    /// Registrers a new user
    /// </summary>
    public partial class RegisterNewUser : System.Web.UI.Page
    {
        
        GeekMeetService wcf = new GeekMeetService();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["action"] == "fromdefaultpage")
            {

                LblFromstartpage.Text = "Du måste registrera dig för att kunna se andras profiler!"
                    + "<h2>Registrera dig nu!</h2>";
            }
            else
            {
                LblFromstartpage.Text = "<h2>Registrera dig nu!</h2>";
            }

        }

        /// <summary>
        /// Creates the profile for the user, but so far without any additional info. 
        /// Triggers when the user clicks continue on creating the profile
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void createProfile(object sender, EventArgs e)
        {
            Guid loggedInUserGUID = (Guid)Membership.GetUser().ProviderUserKey;
            wcf.CreateNewProfile(loggedInUserGUID);

            // Set default profile image (geek.jpg)
            CreateProfileImage(loggedInUserGUID);
        }

        private void CreateProfileImage(Guid loggedInUserGUID)
        {
            string dir = Server.MapPath("~/images/profilePictures/");
            string fileName = loggedInUserGUID + ".jpg"; // Name of the new image

            // Open filestream for default iamge
            FileStream fs = File.Open(
                        dir + "geek.jpg",
                        FileMode.OpenOrCreate,
                        FileAccess.ReadWrite,
                        FileShare.ReadWrite);

            // Open filestream for new image
            FileStream fs2 = File.Open(
              dir + fileName,
              FileMode.OpenOrCreate,
              FileAccess.Write,
              FileShare.ReadWrite);

            // Copy default image to new profile image
            fs.CopyTo(fs2);
        }

        /// <summary>
        /// Log out previously logged in user before account creation
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void signOut(object sender, LoginCancelEventArgs e)
        {
            FormsAuthentication.SignOut();
        }
    }
}